package com.sangmo.fts.process.storage.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.storage.interfaces.CsfStockCreation;
import com.sangmo.fts.process.storage.model.CsfStock;
import com.sangmo.fts.process.storage.model.CsfStockItem;
import com.sangmo.fts.process.storage.model.CsfWaybill;
import com.sangmo.fts.process.storage.model.CsfWaybillItem;
import com.sangmo.fts.process.storage.repo.CsfStockRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CsfStockEntityService extends AuthorityEntityService<CsfStock> {

    @Autowired
    private CsfStockRepo csfStockRepo;

    @Autowired
    private CsfWaybillService csfWaybillService;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private CsfStockItemEntityService csfStockItemEntityService;


    @Override
    protected IDataViewRepo<CsfStock> getRepo() {
        return csfStockRepo;
    }

    @Override
    protected void onCreate(CsfStock entity, Object bo) {
        super.onCreate(entity, bo);
        CsfStockCreation creation = (CsfStockCreation) bo;
        CsfWaybill waybill = csfWaybillService.get(creation.getWaybillId());
        entity.setAgreementId(waybill.getAgreementId());
        entity.setBailor(waybill.getShipper());
        if (null != creation.getConfirmDoc()) {
            fileUploadService.updateRef(creation.getConfirmDoc(), ""+entity.getId(), "CsfStock");
        }
    }

    @Override
    protected void afterCreated(CsfStock entity, Object bo) {
        super.afterCreated(entity, bo);
        CsfStockCreation creation = (CsfStockCreation) bo;
        List<CsfWaybillItem> waybillItems = csfWaybillService.listItemsByWaybillId(creation.getWaybillId());
        List<CsfStockItem> stockItems = buildCsfStockItems(entity.getId(), waybillItems);
        csfStockItemEntityService.saveAll(stockItems);
    }

    private List<CsfStockItem> buildCsfStockItems(Long stockId, List<CsfWaybillItem> waybillItems) {
        List<CsfStockItem> stockItems = new ArrayList<>();
        for (CsfWaybillItem waybillItem : waybillItems) {
            CsfStockItem item = CsfStockItem.fromWaybillItem(waybillItem);
            item.setStockId(stockId);
            stockItems.add(item);
        }
        return stockItems;
    }

    @Override
    protected void onDelete(CsfStock entity) {
        super.onDelete(entity);
        if (!CsfStock.States.PENDING.equals(entity.getState())) {
            throw new ServiceException(StandardErrors.CLIENT_ERROR.getStatus(), "error.csfStock.remove.permit.disabled");
        }
    }
}
